import HomeView from '../views/HomeView.vue'
import { createRouter, createWebHistory } from 'vue-router'
import { isLoggedIn } from '../utils/auth'

const routes = [
  { 
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue'),
    meta: { requiresAuth: false }
  },
  { path: '/register', name: 'register', component: () => import('../views/RegisterView.vue'), meta: { requiresAuth: false } },
  { path: '/profile', name: 'profile', component: () => import('../views/UserProfileView.vue'), meta: { requiresAuth: true } },
  { path: '/address', name: 'address', component: () => import('../views/UserAddressView.vue'), meta: { requiresAuth: true } },
  {
    path: '/',
    name: 'home',
    component: () => import('../views/HomeView.vue'),
    meta: { requiresAuth: true }
  },
  // 校园圈子模块
  {
    path: '/circle',
    name: 'circle',
    component: () => import('../views/circle/IndexView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/circle/posts/:id',
    name: 'postDetail',
    component: () => import('../views/circle/PostDetailView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/circle/create',
    name: 'createPost',
    component: () => import('../views/circle/CreatePostView.vue'),
    meta: { requiresAuth: true }
  },
  // 活动中心模块
  {
    path: '/activity',
    name: 'activity',
    component: () => import('../views/activity/IndexView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/activity/create-club',
    name: 'createClub',
    component: () => import('../views/activity/CreateClubView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/activity/create',
    name: 'createActivity',
    component: () => import('../views/activity/ActivityCreateView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/activity/clubs/:id',
    name: 'clubDetail',
    component: () => import('../views/activity/ClubDetailView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/activity/activities/:id',
    name: 'activityDetail',
    component: () => import('../views/activity/ActivityDetailView.vue'),
    meta: { requiresAuth: true }
  },
  // 跑腿代办模块
  {
    path: '/errand',
    name: 'errand',
    component: () => import('../views/errand/IndexView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/errand/orders/:id',
    name: 'orderDetail',
    component: () => import('../views/errand/OrderDetailView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/errand/create',
    name: 'createOrder',
    component: () => import('../views/errand/CreateOrderView.vue'),
    meta: { requiresAuth: true }
  },
  // 其他页面
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: '404',
    component: () => import('../views/NotFoundView.vue'),
    meta: { requiresAuth: false }
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    if (isLoggedIn()) {
      next()
    } else {
      next({ name: 'login', query: { redirect: to.fullPath } })
    }
  } else {
    next()
  }
})

export default router